 var path = require('path');

 var fs = require('fs');

 module.exports = function (UC, source, config, ArFor, cache, package) {


     var source = config.Source;
     var openjson = config.openjson;

     var UserControl = UC;

     var source = config.Source;
     var getpathjoin = function (_path, file) {
         var u_path = path.join(_path, file);
         var __path = path.join(config.Files.Plugin, '../');
         if (__path.indexOf(u_path) > -1) {
             return u_path;
         } else {
             return u_path;
         }
     };
     var RFile = function (File) {
         fs.exists(File, function (exists) {
             if (exists) {
                 var shell = require('shelljs');
                 shell.rm('-rf', File);
             }
         })
     }

     var RemovePlugin = function (obj, Callback) {
         var localpath = getpathjoin(config.Files.Plugin, obj.localpath);
         var packagepath = getpathjoin(localpath, 'package.json');
         openjson(packagepath, function (error, package) {
             if (error) {
                 Callback(error);
             } else {

                 var _package = config.installPackages[obj.key];
                 package = _package || package;

                 config.unInstallApp(localpath, package, function () {
                     Callback(error, package);
                 })
             }
         })
     }

     var router = function (req, res, next) {

         var body = UserControl.GetForm(req.session.key, req.body);

         var setMessage = function (val) {
             var socketid = UserControl.Userid2Socketid(body.user_id);
             if (socketid) {
                 var Socket = config.Socket;
                 Socket.SetClientMessage(socketid, val)
             };
         } 
         var removePluginOriginal = function (icon, PluginPath, AppsInitFunctions) {
             var PluginOriginal = config.Files.Plugin;
             PluginPath = getpathjoin(PluginOriginal, PluginPath);
             RFile(PluginPath);

             new source("M_673", body, function (obj) {
                 var value = {
                     id: body.id,
                     error: 0,
                     "key": "removed-plugin",
                     notify: {
                         duration: 0,
                         title: '@plug_in_removed',
                         message: package.label,
                         type: 'success'
                     }
                 };
                 if (AppsInitFunctions) {
                     value.AppsInitFunctions = AppsInitFunctions;
                 };
                 config.RemoveFile(icon, function () {
                     setMessage(value)
                 });

                 res.Json({
                     error: 0
                 })
             });
         };




         new source("M_671", body, function (obj) {
             if (obj && obj.localpath) {


                 var localpath = obj.localpath;
                 RemovePlugin(obj, function (error, db) {
                     if (error == null && db.AppsInitFunctions) {
                         config.AppsInitFunctions(db.AppsInitFunctions, function () {
                             removePluginOriginal(obj.icon, localpath, db.AppsInitFunctions);
                         });
                     } else {
                         removePluginOriginal(obj.icon, localpath);
                     }
                 });
             } else {
                 var value = {
                     error: 112
                 };
                 res.Json(value)
             }
         })

     };

     return router;
 }